<?php
  require_once '../../Helpers/DbHelper.php';
  require_once '../../Controllers/AdminController.php';
  require_once '../../Controllers/UserController.php';
  require_once '../../Helpers/PathHelper.php';
  //
  $u = new UserController();
  $u->evaluatePagePermission(UserController::ADMINISTRATOR);
  //
  $c = new AdminController();
  $yearLevelList = $c->getYearLevel();

  if(isset($_POST) && !empty($_POST))
  {
    $teachersName = $_POST['teacher-name'];
    $yearLevel = $_POST['year'];

    $c->addTeacher($teachersName,$yearLevel);
    $teacherList = $c->getTeacherList();
  }
  else
  {
    $teacherList = $c->getTeacherList();
  }
?>
<html>
  <head>
    <title>School Enrollment :: Manage Teachers</title>
    <?php
      require_once '../commons.php';
      renderHeaderStyles() ;
    ?>  </head>
    <body>
    <div class="admin-container">
      <?php renderAdminNav(); ?>
    </div>
  <div class="container">
    <h1>Add Teacher</h1>
    <form name="teachers-form" method="post">
      <table class="labels">
        <tr>
          <td><label for="teacher-name">Add Teacher: </label></td>
          <td><input type="text" name="teacher-name" /></td>
        </tr>
        <tr>
          <td><label for="year-level">Choose a year level: </label></td>
          <td><select name="year">
            <?php
              if(mysql_num_rows($yearLevelList) < 1)
                echo "<option value='0'>No data available</option>";

              while($rows=mysql_fetch_assoc($yearLevelList))
              {
                $i = $rows['YearLevelId'];
                $n = $rows['YearLevelDescription'];
                echo "<option value='$i'>$n</option>";
              }
            ?>
          </select></td>
        </tr>
        <tr><td><input type="submit" name="add-teacher" value="Add Teacher" /></td></tr>
      </table>
    </form><br />
    <hr />
    <table cellspacing="5" align="center" class="table">
      <tr>
        <th width="150">Year Level</th>
        <th>Assigned Teacher</th>
        <th>Operations</th>
      </tr>
      <?php
        if(mysql_num_rows($teacherList) < 1)
          echo "<tr><td colspan='2'>No data available.</td></tr>";
        while($rows = mysql_fetch_assoc($teacherList))
        {
          $yearLevel = $rows['YearLevelDescription'];
          $teacher = $rows['TeachersName'];
          $tid = $rows['TeacherId'];

          echo "<tr>
            <td>$yearLevel</td>
            <td>$teacher</td>
            <td><a href='edit_teacher.php?tid=$tid'>Edit</a></td>
            <td><a href='delete_teacher.php?tid=$tid'>Delete</a></td>
          </tr>";
        }
      ?>
    </table>
  </div>
  </body>
</html>